<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable style="width: 200px;" placeholder="请输入学生姓名" />
      <el-date-picker
        v-model="listQuery.searchTime"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd hh:mm"
        value-format="yyyy-MM-dd hh:mm"
        @change="selectTime" />
      <el-button type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button :disabled="multipleSelection.length===0" type="danger" icon="el-icon-delete" @click="handleDeleteAll">批量删除</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="ID" prop="id" />

      <el-table-column align="center" label="评价老师" prop="poster" />

      <el-table-column align="center" label="评价内容" prop="content" />

      <el-table-column align="center" label="被评学生" prop="studentName" />

      <el-table-column align="center" label="评价时间" prop="created" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleReply(scope.row)">详情</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <!-- 评论回复 -->
    <el-dialog :visible.sync="replyFormVisible" title="评价详情">
      <el-form label-position="left" label-width="100px" style="width:500px; margin-left:50px;">
        <el-form-item label="评价老师">{{ dataForm.poster }}</el-form-item>
        <el-form-item label="评价内容">{{ dataForm.content }}</el-form-item>
        <el-form-item label="被评会员">{{ dataForm.studentName }}</el-form-item>
        <el-form-item label="评价时间">{{ dataForm.created }}</el-form-item>
        <el-form-item label="图片">
          <el-image
            v-for="(item,index) in dataForm.postImageList"
            :key="index"
            :src="item.url"
            :preview-src-list="dataForm.srcList"
            style="width: 100px; height: 100px;margin-right:20px;margin-bottom:20px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="replyFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComment,
  deleteComment
} from '@/api/comment'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Comment',
  components: {
    Pagination
  },
  data() {
    return {
      dataForm: {},
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        type: 2,
        size: 10,
        index: 0,
        name: '',
        startDate: '',
        endDate: '',
        searchTime: []
      },
      replyFormVisible: false,
      value1: '',
      multipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      if (e && e.page) {
        this.listQuery.index = e.page - 1
      }
      this.listLoading = true
      const data = Object.assign({}, this.listQuery)
      delete data.searchTime
      listComment(data)
        .then(response => {
          const content = response.data.data.content
          if (content.length) {
            content.forEach((item, index) => {
              item[`srcList`] = []
              if (item.postImageList.length) {
                item.postImageList.map(r => {
                  item[`srcList`].push(r.url)
                })
              }
            })
          }
          this.list = content
          this.total = response.data.data.totalRecords
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReply(row) {
      this.replyFormVisible = true
      this.dataForm = Object.assign(this.dataForm, row)
    },
    handleDeleteAll() {
      const idList = []
      this.multipleSelection.forEach(item => {
        idList.push(item.id)
      })
      deleteComment({
        idList
      }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const params = {
            idList: [id]
          }
          deleteComment(params).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        })
        .catch(e => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    selectTime(val) {
      if (val) {
        this.listQuery.startDate = val[0]
        this.listQuery.endDate = val[1]
      } else {
        this.listQuery.startDate = ''
        this.listQuery.endDate = ''
      }
    }
  }
}
</script>
<style scoped>
	.txt {
		line-height: 32px;
	}
</style>
